<template>
  <div class="item" style="width: 100%">
    <div class="name">{{ item.name }}</div>
    <div class="manager">{{ item.manager }}</div>
    <el-dropdown class="dropdown" @command="commandEvent">
      <span>
        <span>操作</span>
        <i v-xxx class="el-icon-arrow-down el-icon--down" />
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="add">添加子部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!item.top"
            command="look"
          >查看部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!item.top"
            command="del"
          >删除部门</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script>
import { companyDepartmentDelete } from '@/api/departments.js'
export default {
  name: 'TreeItem',
  props: {
    item: {
      type: Object,
      // 基本数据类型(String,Number,undefined,null,boolean...)
      // dafault:直接写，复杂数据类型：()=>{return 复制数据类型}
      default: () => ({}),
      require: true,
      validator: (value) => {
        return !!value.name
      }
    }
  },
  methods: {
    commandEvent(command) {
      switch (command) {
        case 'add':
          this.$bus.$emit('showDialog', true, this.item, 'add') // 将当前点击项数据传递给弹框组件
          // console.log('添加')
          break
        case 'look':
          this.$bus.$emit('showDialog', true, this.item, 'edit')
          console.log('查看', this.item)
          break
        case 'del':
          console.log('删除')
          // 弹出提示确认框 如果点击了确定 进行删除api调用 调用成功 提示与刷新父级列表数据
          this.$confirm(`确定删除该部门:[ ${this.item.name} ]吗`, '提示')
            .then(async() => {
              await companyDepartmentDelete(this.item.id)
              this.$message.success('删除成功')
              this.$emit('getData')
            })
            .catch(() => {})
          break
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.item {
  display: flex;
  align-items: center;
  .name {
    flex: 1;
  }
  .manager {
    width: 120px;
    margin-right: 20px;
    text-align: right;
  }
}
</style>
